package com.itheima.myshopuser.controller;

import com.itheima.myshopuser.pojo.User;
import com.itheima.myshopuser.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @File: UserController
 * @Brief: 用户控制类
 * @Details:
 * @Author: Kevin
 * @Date: 2024/10/22 15:45
 * @Version: 1.0
 * @Department: 人工智能与仿真实验室
 * @Copyright: Copyright By AI&Simulation Lab, All Rights Reserved
 */
@RequestMapping("/user")
@RestController
/*** 用户Controller */
//@Api(description = "用户控制器")
public class UserController {
    @Autowired
    private UserService userService;
    /*** 查询所有用户     */
    @RequestMapping(method = RequestMethod.GET)
    public List<User> findAll(){
        return userService.findAll();
    }
    private static final Log log = LogFactory.getLog(UserController.class);
    /*** 根据id查询用户     */
    @ApiOperation(value = "查询主键用户")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public User findById(@PathVariable Integer id){
        log.info("进入了 UserController 的 findById 方法");
        if (id % 3 == 1){
            System.out.println("用户微服务1111");
        } else if (id % 3 == 2){
            System.out.println("用户微服务2222");
        } else {
            System.out.println("用户微服务");
        }
        return userService.findById(id);    }
    /*** 添加用户     */
    @RequestMapping(method = RequestMethod.POST)
    public String add(@RequestBody User user){
        userService.add(user);
        return "添加成功";    }
    /*** 修改用户     */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public String update(@RequestBody User user,@PathVariable Integer id){
        //设置id
        user.setId(id);
        userService.update(user);
        return "修改成功";    }
    /*** 根据id删除用户     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public String deleteById(@PathVariable Integer id){
        userService.deleteById(id);
        return "删除成功";    }
}